const express = require('express')
const morgan = require('morgan')
const globalErrorHandler = require('./middlewares/globalErrorHandler')
const unknownRouterHandler = require('./middlewares/unknownRouterHandler')
const visitLogger = require('./middlewares/visitLogger')
const adminRouter = require('./routes/adminRouter')
const captchaRouter = require('./routes/captchaRouter')
const articleRouter = require('./routes/articleRouter')
const categoryRouter = require('./routes/categoryRouter')
const tagRouter = require('./routes/tagRouter')
const statsRouter = require('./routes/statsRouter')
const imageRouter = require('./routes/imageRouter')

const app = express()

// 中间件
app.use(morgan('dev'))
app.use(express.json())
app.use(visitLogger)

// 路由注册
app.use(adminRouter)
app.use(captchaRouter)
app.use(articleRouter)
app.use(categoryRouter)
app.use(tagRouter)
app.use(statsRouter)
app.use(imageRouter)

// 处理未知路由
app.all('{*splat}', unknownRouterHandler)

// 错误处理中间件
app.use(globalErrorHandler)

module.exports = app
